<?php

/**
 * 2017年6月6日22:29:38
 * PDO SAFE
 */

include 'common.php';

$username = $_COOKIE['username'];
$token = $_COOKIE['token'];

$stime = $_REQUEST['stime'];
try {	
	$dbc = pdo_root_connect();
	
	$wishlist = array();
	
	$time = time();
	
	$stmt = $dbc->prepare('select * from userinfo where username=:username and token=:token');
	
	$stmt->bindParam(':username', $username);
	$stmt->bindParam(':token', $token);
	$stmt->execute();
	//$result = mysql_query("select * from userinfo where username='$username' and token='$token'");
	
	if($stmt->rowCount() == 0){
		$username = '';
	}
	
	if(is_null($stime) || $stime == ''){
		$stime = $time;
	}
	$stmt2 = $dbc->prepare('select * from userwish where status=0 and publishtime>0 and publishtime<:stime order by status,publishtime desc limit 20');
	$stmt2->bindParam(':stime', $stime);
	
	$stmt2->execute();
	while($row = $stmt2->fetch()){
		$currentid = $row['id'];
		$is_mine = $username == '' ? false : strcasecmp($username, $row['username']) == 0;
		$doliked = $username == '' ? false : $dbc->query("select * from dolikecheck where username='$username' and like2id='$currentid'")->rowCount() > 0;
		array_push($wishlist, ['id'=>(int)$currentid, 'type'=>(int)$row['hopetype'],
				'time'=>(int)$row['uploadtime'], 'finishedtime'=>(int)$row['finishedtime'],
				'publishtime'=>(int)$row['publishtime'], 'good'=>(int)$row['good'],
				'is_mine'=>(boolean)$is_mine, 'liked'=>(boolean)$doliked,
				'hope'=>$row['msg']]);
	}
	$errorcode = 1;
}catch (PDOException $e){
	$errorcode = 4;
}
echo json_encode(array('errorcode'=>$errorcode, 'wishlist'=>$wishlist));
?>
